﻿using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

namespace Linker.TaskCenter.Migrations
{
    public partial class UpdateEntityPK : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_task_service_ServiceId",
                table: "task");

            migrationBuilder.DropPrimaryKey(
                name: "PK_task",
                table: "task");

            migrationBuilder.DropIndex(
                name: "IX_task_TaskUId",
                table: "task");

            migrationBuilder.DropPrimaryKey(
                name: "PK_service",
                table: "service");

            migrationBuilder.DropColumn(
                name: "TaskUId",
                table: "task");

            migrationBuilder.DropColumn(
                name: "SystemId",
                table: "service");

            migrationBuilder.RenameTable(
                name: "task",
                newName: "Tasks");

            migrationBuilder.RenameTable(
                name: "service",
                newName: "Services");

            migrationBuilder.RenameIndex(
                name: "IX_task_ServiceId",
                table: "Tasks",
                newName: "IX_Tasks_ServiceId");

            migrationBuilder.RenameColumn(
                name: "SystemName",
                table: "Services",
                newName: "ServerName");

            migrationBuilder.AlterColumn<Guid>(
                name: "ServiceId",
                table: "Tasks",
                type: "char(36)",
                nullable: false,
                oldClrType: typeof(long),
                oldType: "bigint");

            migrationBuilder.AlterColumn<Guid>(
                name: "Id",
                table: "Tasks",
                type: "char(36)",
                nullable: false,
                oldClrType: typeof(long),
                oldType: "bigint")
                .OldAnnotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn);

            migrationBuilder.AlterColumn<Guid>(
                name: "Id",
                table: "Services",
                type: "char(36)",
                nullable: false,
                oldClrType: typeof(long),
                oldType: "bigint")
                .OldAnnotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn);

            migrationBuilder.AddPrimaryKey(
                name: "PK_Tasks",
                table: "Tasks",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_Services",
                table: "Services",
                column: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_Tasks_Services_ServiceId",
                table: "Tasks",
                column: "ServiceId",
                principalTable: "Services",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Tasks_Services_ServiceId",
                table: "Tasks");

            migrationBuilder.DropPrimaryKey(
                name: "PK_Tasks",
                table: "Tasks");

            migrationBuilder.DropPrimaryKey(
                name: "PK_Services",
                table: "Services");

            migrationBuilder.RenameTable(
                name: "Tasks",
                newName: "task");

            migrationBuilder.RenameTable(
                name: "Services",
                newName: "service");

            migrationBuilder.RenameIndex(
                name: "IX_Tasks_ServiceId",
                table: "task",
                newName: "IX_task_ServiceId");

            migrationBuilder.RenameColumn(
                name: "ServerName",
                table: "service",
                newName: "SystemName");

            migrationBuilder.AlterColumn<long>(
                name: "ServiceId",
                table: "task",
                type: "bigint",
                nullable: false,
                oldClrType: typeof(Guid),
                oldType: "char(36)");

            migrationBuilder.AlterColumn<long>(
                name: "Id",
                table: "task",
                type: "bigint",
                nullable: false,
                oldClrType: typeof(Guid),
                oldType: "char(36)")
                .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn);

            migrationBuilder.AddColumn<Guid>(
                name: "TaskUId",
                table: "task",
                type: "char(36)",
                nullable: false,
                defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));

            migrationBuilder.AlterColumn<long>(
                name: "Id",
                table: "service",
                type: "bigint",
                nullable: false,
                oldClrType: typeof(Guid),
                oldType: "char(36)")
                .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn);

            migrationBuilder.AddColumn<Guid>(
                name: "SystemId",
                table: "service",
                type: "char(36)",
                nullable: false,
                defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));

            migrationBuilder.AddPrimaryKey(
                name: "PK_task",
                table: "task",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_service",
                table: "service",
                column: "Id");

            migrationBuilder.CreateIndex(
                name: "IX_task_TaskUId",
                table: "task",
                column: "TaskUId",
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_task_service_ServiceId",
                table: "task",
                column: "ServiceId",
                principalTable: "service",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }
    }
}
